package Leetcode.LeetcodeDemo1;

public class L200 {
    public static int numIslands(char[][] grid) {
        if((grid==null)||(grid[0]==null)){
            return 0;
        }
        int res=0;
        int N=grid.length;
        int M=grid[0].length;
        for(int i=0;i<N;i++){
            for(int j=0;j<M;j++){
                if(grid[i][j]=='1'){
                    infect(grid,i,j,N,M);
                }
                res++;
            }
        }
        return res;

    }
    public static void infect(char[][] m,int i,int j,int N ,int M){
        if(i<0||i>=N||j<0||j>=M||m[i][j]!='1'){
            return;
        }
        m[i][j]='2';
        infect(m,i+1,j,N,M);
        infect(m,i-1,j,N,M);
        infect(m,i,j+1,N,M);
        infect(m,i,j-1,N,M);
    }

    public static void main(String[] args) {
        char[][] grid={{'1','1','1','1','0'},
                       {'1','1','0','1','0'},
                       {'1','1','0','0','0'},
                       {'0','0','0','0','0'}};
        numIslands(grid);
    }
}
